package cn.sibu.myapplication.VedioCaptrue;

import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

import java.io.File;
import java.io.IOException;

import cn.sibu.myapplication.R;



public class VideoCaptrueActivity extends AppCompatActivity implements SurfaceHolder.Callback {

    private Camera mCamera;
    private MediaRecorder mReCorder;

    private SurfaceView mPreview;
    private Button mRecordButton;
    private boolean mRecording = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_captrue);
        mRecordButton = (Button) findViewById(R.id.button_record);
        mRecordButton.setText("Start Recording");

        mPreview = (SurfaceView) findViewById(R.id.surface_video);
        mPreview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        mCamera =Camera.open();
        //旋转预览画面为竖屏
        mCamera.setDisplayOrientation(90);
        mReCorder = new MediaRecorder();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCamera.release();
        mCamera = null;

    }
    public void onRecordClick(View v){
            updateRecordingState();
    }

    private void initializeRecorder()throws IllegalStateException,IOException{
        //解锁摄像头，允许MediaRecorder使用它
        mCamera.unlock();
        mReCorder.setCamera(mCamera);
        //设置MediaRecorder的数据源
        mReCorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        mReCorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        //更新输出设置
        File recorderOutput = new File(Environment.getExternalStorageDirectory(),"recorder_video.mp4");
        if(recorderOutput.exists()){
            recorderOutput.delete();
        }
        CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
        mReCorder.setProfile(cpHigh);
        mReCorder.setOutputFile(recorderOutput.getAbsolutePath());
        //为摄像机关联一个surface,从而实现在录制的同时可以预览
        mReCorder.setPreviewDisplay(mPreview.getHolder().getSurface());

        //设置录制的一些限制
        mReCorder.setMaxDuration(50000);//50秒
        mReCorder.setMaxFileSize(5000000);//大约5M
        mReCorder.prepare();
    }

    private void updateRecordingState(){
        if(mRecording){
            mRecording  =false;
            //重置摄像机的状态以便进行下次录制
            mReCorder.stop();
            mReCorder.reset();
            //返回摄像机继续预览
            mCamera.lock();
            mRecordButton.setText("Start Recording");
        }else {

            try {
                //重置摄像机以便进行下次会话
                initializeRecorder();
                //开始录制
                mRecording = true;
                mReCorder.start();
                mRecordButton.setText("Stop Recording");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
            //得到一个surface后，立即启动摄像头预览
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

}
